﻿using System;
using System.Collections.Generic;
using System.Data.Linq;
using System.Linq;
using System.Web.Mvc;
using InfoTech.Core.Service;
using InfoTech.LanTranh.MucVu.Bussiness;

namespace InfoTech.LanTranh.MucVu.Service
{
    public class GIAOHATService : BaseService
    {
        public static bool Check_Insert_GIAOHAT(GIAOHAT giaohat)
        {
            bool result = true;
            DataContext dataContext = CreateDataContext();
            result &= dataContext.GetTable<GIAOHAT>().FirstOrDefault(c => c.TENGIAOHAT.ToUpper() == giaohat.TENGIAOHAT.ToUpper()
                            && c.MAGIAOPHAN == giaohat.MAGIAOPHAN) == null;
            return result;
        }

        public static bool Check_Update_GIAOHAT(GIAOHAT giaohat)
        {
            bool result = true;
            DataContext dataContext = CreateDataContext();
            result &= dataContext.GetTable<GIAOHAT>().FirstOrDefault(c => c.TENGIAOHAT.ToUpper() == giaohat.TENGIAOHAT.ToUpper()
                            && c.MAGIAOPHAN == giaohat.MAGIAOHAT && c.MAGIAOPHAN != giaohat.MAGIAOHAT) == null;
            return result;
        }

        public static bool Check_Delete_GIAOHAT(string[] strings)
        {
            bool result = true;
            DataContext dataContext = CreateDataContext();
            result &= dataContext.GetTable<GIAOXU>().FirstOrDefault(c => strings.Contains(c.MAGIAOHAT + "")) == null;
            return result;
        }

        public static IEnumerable<SelectListItem> Get_List_Join_GiaoPhan()
        {
            DataContext dataContext = CreateDataContext();
            List<SelectListItem> list = new List<SelectListItem>();
            list.Add(new SelectListItem() { Text = "--- {Chọn Giáo hạt} ---", Value = "" });
            list.AddRange(new SelectList(dataContext.GetTable<GIAOHAT>()
                .Join(dataContext.GetTable<GIAOPHAN>(), c => c.MAGIAOPHAN, v => v.MAGIAOPHAN,
                    (c, v) => new { c.MAGIAOHAT, TENGIAOHAT = c.TENGIAOHAT + " - " + v.TENGIAOPHAN })
                    .OrderBy(c => c.TENGIAOHAT)
                , "MAGIAOHAT", "TENGIAOHAT"));
            return list;
        }

        public static IEnumerable<SelectListItem> Get_List_Join_GiaoPhan_Data()
        {
            DataContext dataContext = CreateDataContext();
            List<SelectListItem> list = new List<SelectListItem>();
            list.Add(new SelectListItem() { Text = "--- {Chọn Giáo hạt} ---", Value = "" });
            list.AddRange(dataContext.GetTable<GIAOHAT>().Where(c => c.ISDATA.HasValue ? c.ISDATA.Value : false)
                .Join(dataContext.GetTable<GIAOPHAN>().Where(c => c.ISDATA.HasValue ? c.ISDATA.Value : false), c => c.MAGIAOPHAN, v => v.MAGIAOPHAN,
                    (c, v) => new { c.MAGIAOHAT, TENGIAOHAT = c.TENGIAOHAT + " - " + v.TENGIAOPHAN, c.ISDEFAULT })
                    .OrderBy(c => c.TENGIAOHAT).Select(c => new SelectListItem() { Text = c.TENGIAOHAT, Value = c.MAGIAOHAT + "", Selected = c.ISDEFAULT ?? false }));
            return list;
        }

        public static IEnumerable<SelectListItem> Get_List_Join_GiaoPhan(string id)
        {
            DataContext dataContext = CreateDataContext();
            List<SelectListItem> list = new List<SelectListItem>();
            list.Add(new SelectListItem() { Text = "--- {Chọn Giáo hạt} ---", Value = "" });
            list.AddRange(new SelectList(dataContext.GetTable<GIAOHAT>()
                .Join(dataContext.GetTable<GIAOPHAN>().Where(c => c.MAGIAOPHAN + "" == id), c => c.MAGIAOPHAN, v => v.MAGIAOPHAN,
                    (c, v) => new { c.MAGIAOHAT, c.TENGIAOHAT })
                    .OrderBy(c => c.TENGIAOHAT)
                , "MAGIAOHAT", "TENGIAOHAT"));
            return list;
        }
    }
}
